# Maintainer: Tim Meusel <tim@bastelfreak.de>
# Maintainer: Andreas 'Segaja' Schleifer <segaja at archlinux dot org>

pkgname=(ruby-sinatra ruby-sinatra-contrib ruby-rack-protection)
pkgver=2.2.2
pkgrel=2
arch=('any')
url='https://sinatrarb.com/'
license=('MIT')
makedepends=('ruby')
checkdepends=(
    'asciidoctor'
    'ruby-activesupport'
    'ruby-builder'
    'ruby-erubi'
    'ruby-kramdown'
    'ruby-maruku'
    'ruby-mustermann'
    'ruby-nokogiri'
    'ruby-rack'
    'ruby-rack-protection'
    'ruby-rack-test'
    'ruby-rake'
    'ruby-rdiscount'
    'ruby-rspec'
    'ruby-sass'
    'ruby-sinatra-contrib'
    'ruby-tilt'
    # ruby-sinatra - integration tests
    #'ruby-puma'
    #'ruby-thin'
    #'ruby-webrick'
)
options=('!emptydirs')
source=("https://github.com/sinatra/sinatra/archive/v${pkgver}/sinatra-v${pkgver}.tar.gz")
sha512sums=('e69a36738a7c1095968a343113ddba414ff685e941a8b55575e4b2ffa5a8e7394d874966d5532643cdfec997ac4c825c293185f6688c715006e37159eedd2be8')

prepare() {
  cd "sinatra-${pkgver}"

  # update gemspec to allow newer version of the dependencies
  sed --in-place --regexp-extended 's|~>|>=|g' sinatra.gemspec */*.gemspec
}

build() {
  cd "sinatra-${pkgver}"

  gem build sinatra.gemspec

  (cd sinatra-contrib
    gem build sinatra-contrib.gemspec
  )

  (cd rack-protection
    gem build rack-protection.gemspec
  )
}

check() {
  cd "sinatra-${pkgver}"

  # cannot load such file -- coffee-script: skipping coffee tests
  # cannot load such file -- creole: skipping creole tests
  # cannot load such file -- erubis: skipping erubis tests
  # cannot load such file -- haml: skipping haml tests
  # rainbows is not installed, skipping integration tests
  # rainbows is not installed, skipping integration tests
  # reel is not installed, skipping integration tests
  # HTTP is not installed, skipping integration tests
  # cannot load such file -- less: skipping less tests
  # cannot load such file -- liquid: skipping liquid tests
  # cannot load such file -- markaby: skipping markaby tests
  # cannot load such file -- pandoc-ruby: skipping markdown tests with Tilt::PandocTemplate
  # cannot load such file -- commonmarker: skipping markdown tests with Tilt::CommonMarkerTemplate
  # cannot load such file -- redcarpet: skipping markdown tests with Tilt::RedcarpetTemplate
  # cannot load such file -- bluecloth: skipping markdown tests with Tilt::BlueClothTemplate
  # cannot load such file -- wikicloth: skipping mediawiki tests
  # cannot load such file -- rabl: skipping rabl tests
  # cannot load such file -- radius: skipping radius tests
  # cannot load such file -- slim: skipping slim tests
  # cannot load such file -- stylus: skipping stylus tests
  # cannot load such file -- redcloth: skipping textile tests
  # cannot load such file -- wlang: skipping wlang tests
  # cannot load such file -- yajl: skipping yajl tests
  echo "sinatra"
  rake spec

  # disabled as it requires hamlit
  #(cd sinatra-contrib
  #  echo "sinatra"
  #  rake spec
  #)

  (cd rack-protection
    echo "rack-protection"
    rake spec
  )
}

package_ruby-sinatra() {
  pkgdesc='Classy web-development dressed in a DSL'
  depends=('ruby' 'ruby-mustermann' 'ruby-rack' 'ruby-rack-protection' 'ruby-tilt')

  _package "sinatra" "LICENSE"
}

package_ruby-sinatra-contrib() {
  pkgdesc='Collection of useful Sinatra extensions'
  depends=('ruby' 'ruby-multi_json' 'ruby-mustermann' 'ruby-rack-protection' 'ruby-sinatra' 'ruby-tilt')

  _package "sinatra-contrib" "LICENSE"
}

package_ruby-rack-protection() {
  pkgdesc='Protect against typical web attacks, works with all Rack apps, including Rails'
  depends=('ruby' 'ruby-rack')

  _package "rack-protection" "License"
}

_package() {
  local _gemdir="$(gem env gemdir)"
  local _gemname="${1}"
  local _licensefile="${2}"

  cd "sinatra-${pkgver}"

  if [ "${_gemname}" != "sinatra" ]; then
    cd "${_gemname}"
  fi

  gem install \
    --local \
    --verbose \
    --ignore-dependencies \
    --no-user-install \
    --install-dir "${pkgdir}/${_gemdir}" \
    --bindir "${pkgdir}/usr/bin" \
    "${_gemname}-${pkgver}.gem"

  # remove unrepreducible files
  rm -frv \
    "${pkgdir}/${_gemdir}/cache/" \
    "${pkgdir}/${_gemdir}/gems/${_gemname}-${pkgver}/vendor/" \
    "${pkgdir}/${_gemdir}/doc/${_gemname}-${pkgver}/ri/ext/"

  find "${pkgdir}/${_gemdir}/gems/" \
    -type f \
    \( \
      -iname "*.o" -o \
      -iname "*.c" -o \
      -iname "*.so" -o \
      -iname "*.time" -o \
      -iname "gem.build_complete" -o \
      -iname "Makefile" \
    \) \
    -delete

  find "${pkgdir}/${_gemdir}/extensions/" \
    -type f \
    \( \
      -iname "mkmf.log" -o \
      -iname "gem_make.out" \
    \) \
    -delete

  install -Dm 644 "${_licensefile}" --target-directory "${pkgdir}/usr/share/licenses/ruby-${_gemname}/"
  install -Dm 644 *.md --target-directory "${pkgdir}/usr/share/doc/ruby-${_gemname}"
}
